
package com.tools.cleanmaster.model.db;

import android.database.Cursor;

import com.tools.cleanmaster.model.db.annotations.TrashDBAnnotation;
import com.tools.cleanmaster.model.db.annotations.Type;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ResidualTrashBean {

    @TrashDBAnnotation(name = "a", type = Type.IntData)
    public int id;

    @TrashDBAnnotation(name = "c", type = Type.StringData)
    public String pkgNames;

    @TrashDBAnnotation(name = "d", type = Type.StringData)
    public String softName;

    @TrashDBAnnotation(name = "e", type = Type.StringData)
    public String filePath;

    @TrashDBAnnotation(name = "f", type = Type.StringData)
    public String alertInfo;

    @TrashDBAnnotation(name = "h", type = Type.IntData)
    public int deleteLevel;

    @TrashDBAnnotation(name = "i", type = Type.LongData)
    public long fileId;


    public String rootPath;

    public int lid = 1;//保存在a表中语言ID

    /**
     * 更新时间
     */
    public long updateTime;

    /**
     * 路径来源０本地1 云端
     */
    public int sourceType;

    public String[] getPkgs() {
        return pkgNames.split("[+]");
    }

    /**
     * 变换 Cursor 为 List<ResidualTrashBean>
     *
     * @param cursor Cursor
     * @param filter 过滤器，null 表示不进行过滤
     * @return List<ResidualTrashBean>
     */
    public static List<ResidualTrashBean> fromCursor(Cursor cursor, DBBeanFilter<ResidualTrashBean> filter) {
        if (cursor == null || cursor.getCount() == 0) {
            return Collections.emptyList();
        }

        final int idIndex = cursor.getColumnIndexOrThrow("a");
        final int pkgNamesIndex = cursor.getColumnIndexOrThrow("c");
        final int softNameIndex = cursor.getColumnIndexOrThrow("d");
        final int filePathIndex = cursor.getColumnIndexOrThrow("e");
        final int alertInfoIndex = cursor.getColumnIndexOrThrow("f");
        final int deleteLevelIndex = cursor.getColumnIndexOrThrow("h");
        final int fileIdIndex = cursor.getColumnIndexOrThrow("i");

        ArrayList<ResidualTrashBean> beans = new ArrayList<ResidualTrashBean>(cursor.getCount());
        while (cursor.moveToNext()) {
            ResidualTrashBean bean = new ResidualTrashBean();
            bean.id = cursor.getInt(idIndex);
            bean.pkgNames = cursor.getString(pkgNamesIndex);
            bean.softName = cursor.getString(softNameIndex);
            bean.filePath = cursor.getString(filePathIndex);
            bean.alertInfo = cursor.getString(alertInfoIndex);
            bean.deleteLevel = cursor.getInt(deleteLevelIndex);
            bean.fileId = cursor.getLong(fileIdIndex);

            if (filter != null && filter.needFilter(bean)) {
                continue;
            }

            beans.add(bean);
        }

        return beans;
    }
}

